//
//  AuthorizationManager.m
//  18Birdies
//
//  Created by weihuazhang on 2016/11/29.
//  Copyright © 2016年 Lc. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import "AuthorizationManager.h"
#import "LcFoundation.h"
#import "ViewControllerManager.h"
#import "LcPhotoCenter.h"
#import "LcMediaCenter.h"
#import "UserManager.h"

@interface AuthorizationManager ()

@end

@implementation AuthorizationManager

- (BOOL)needShowAuthorization:(BOOL)isSignUp {
  if (isSignUp) {
    return YES;
  }
  // check gps
  if ([[LcLocationCenter sharedInstance] authorizationNotDetermined]) {
    return YES;
  }
  return NO;
}

- (void)openSettingPage {
  [[UIApplication sharedApplication]
                openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]
                options:@{}
      completionHandler:nil];
}

- (void)useSystemPhoto:(AuthorizationUseHandle)authorizationUseHandle {
  LcPhotoCenter* photoCenter = [LcPhotoCenter sharedInstance];
  BOOL accessGranted = [photoCenter authorized];
  if (accessGranted) {
    if (authorizationUseHandle) {
      authorizationUseHandle(accessGranted);
    }
  } else {
    if ([photoCenter authorizationNotDetermined]) {
      // not determined
      accessGranted = [photoCenter requestAuthorization];
    } else {
      [self openSettingPage];
    }
    if (authorizationUseHandle) {
      authorizationUseHandle(accessGranted);
    }
  }
}

- (void)useSystemCamera:(AuthorizationUseHandle)authorizationUseHandle {
  LcMediaCenter* mediaCenter = [LcMediaCenter sharedInstance];
  BOOL accessGranted = [mediaCenter authorized:AVMediaTypeVideo];
  if (accessGranted) {
    if (authorizationUseHandle) {
      authorizationUseHandle(accessGranted);
    }
  } else {
    [UIView hideKeyBoard];

    // click enable
    if ([mediaCenter authorizationNotDetermined:AVMediaTypeVideo]) {
      // not determined
      accessGranted = [mediaCenter requestAuthorization:AVMediaTypeVideo];
    } else {
      [self openSettingPage];
    }
    if (authorizationUseHandle) {
      authorizationUseHandle(accessGranted);
    }
  }
}

- (void)useSystemPhotoAndCamera:(AuthorizationUseWithTypeHandle)authorizationUseWithTypeHandle {
  LcMediaCenter* mediaCenter = [LcMediaCenter sharedInstance];
  LcPhotoCenter* photoCenter = [LcPhotoCenter sharedInstance];
  LC_CHECK_TRUE(![mediaCenter authorized:AVMediaTypeVideo] || ![photoCenter authorized]);
  [UIView hideKeyBoard];
  // Photo
  BOOL photoAccessGranted = NO;
  if ([photoCenter authorized]) {
    photoAccessGranted = YES;
  } else if ([photoCenter authorizationNotDetermined]) {
    // not determined
    photoAccessGranted = [photoCenter requestAuthorization];
  } else {
    [self openSettingPage];
  }
  if (authorizationUseWithTypeHandle) {
    authorizationUseWithTypeHandle(AUTHORIZATION_PHOTO, photoAccessGranted);
  }
  // Video
  BOOL videoAccessGranted = NO;
  if ([mediaCenter authorized:AVMediaTypeVideo]) {
    videoAccessGranted = YES;
  } else if ([mediaCenter authorizationNotDetermined:AVMediaTypeVideo]) {
    // not determined
    videoAccessGranted = [mediaCenter requestAuthorization:AVMediaTypeVideo];
  } else {
    [self openSettingPage];
  }
  if (authorizationUseWithTypeHandle) {
    authorizationUseWithTypeHandle(AUTHORIZATION_CAMERA, videoAccessGranted);
  }
}

- (void)useSystemMicrophone:(AuthorizationUseHandle)authorizationUseHandle {
  LC_CHECK_NOT_NULL(authorizationUseHandle);

  LcMediaCenter* mediaCenter = [LcMediaCenter sharedInstance];
  BOOL accessGranted = [mediaCenter authorized:AVMediaTypeAudio];
  if (accessGranted) {
    if (authorizationUseHandle) {
      authorizationUseHandle(accessGranted);
    }
  } else {
    [UIView hideKeyBoard];
    // click enable
    if ([mediaCenter authorizationNotDetermined:AVMediaTypeAudio]) {
      // not determined
      accessGranted = [mediaCenter requestAuthorization:AVMediaTypeAudio];
    } else {
      [self openSettingPage];
    }
    if (authorizationUseHandle) {
      authorizationUseHandle(accessGranted);
    }
  }
}

- (void)useSystemLocation:(AuthorizationSkippedHandle)authorizationUseHandle {
  LcLocationCenter* locationCenter = [LcLocationCenter sharedInstance];
  BOOL accessGranted = [locationCenter authorized];
  if (accessGranted) {
    if (authorizationUseHandle) {
      authorizationUseHandle(accessGranted, NO);
    }
  } else {
    [UIView hideKeyBoard];
    // click enable
    if ([locationCenter authorizationNotDetermined]) {
      // not determined
      [locationCenter requestAuthorization:^(BOOL granted) {
        if (granted) {
          //          [[LcNotificationCenter sharedInstance]
          //              sendNotification:SVNotification.LOCATION_PERMISSION_CHANGED];
        }
        if (authorizationUseHandle) {
          authorizationUseHandle(granted, NO);
        }
      }];
    } else {
      [self openSettingPage];
      if (authorizationUseHandle) {
        authorizationUseHandle(accessGranted, NO);
      }
    }
  }
}

- (void)doRequestLocationAccess:(AuthorizationUseHandle)authorizationUseHandle {
  LcLocationCenter* locationCenter = [LcLocationCenter sharedInstance];
  if ([locationCenter authorizationNotDetermined]) {
    [locationCenter requestAuthorization:^(BOOL granted) {
      if (authorizationUseHandle) {
        authorizationUseHandle(granted);
      }
    }];
  } else {
    [self openSettingPage];
    if (authorizationUseHandle) {
      authorizationUseHandle([locationCenter authorized]);
    }
  }
}

@end
